Simple hipchat bot in go
This is some very simple hipchat bot code in golang. It will tell you the time in a few locations when you mention it.
package main
import "github.com/daneharrigan/hipchat"
import "fmt"
import "time"
import "strings"
import "log"
func main() {
user := "NNNNN_NNNNNN"
pass := "password"
resource := "bot" //should be bot
roomJid := "[email protected]"
fullName := "mr bot" //seems to need to be lowercase
mentionName := "mrbot"
client, err := hipchat.NewClient(user, pass, resource)
if err != nil {
fmt.Printf("client error: %s\n", err)
return
}
fmt.Printf("Connected\n")
rooms := client.Rooms()
for _, element := range rooms {
fmt.Printf("Room: %s\n", element.Id)
client.Join(element.Id, fullName)
}
client.Status("chat")
// for _,element := range rooms {
// client.Say(element.Id, fullName, "Hello")
// }
for message := range client.Messages() {
if strings.HasPrefix(message.Body, "@"+mentionName) {
t := time.Now()
gmt, er1 := time.LoadLocation("Europe/London")
sf, er2 := time.LoadLocation("America/Los_Angeles")
if er1 != nil {
log.Panic(err)
}
if er2 != nil {
log.Panic(err)
}
timestr := t.Format("Jan 2, 2006 at 3:04pm (MST)")
client.Say(roomJid, fullName, "The time in Japan is:"+timestr)
t = t.In(gmt)
timestr = t.Format("Jan 2, 2006 at 3:04pm (MST)")
client.Say(roomJid, fullName, "The time in London (and Dublin) is:"+timestr)
t = t.In(sf)
timestr = t.Format("Jan 2, 2006 at 3:04pm (MST)")
client.Say(roomJid, fullName, "The time in San Francisco is:"+timestr)
}
}
select {}
}